//
//  DetailViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DetailViewController.h"
#import "LTView.h"
#import "MapViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface DetailViewController ()

@property(nonatomic, strong)LTView *nameView;
@property(nonatomic, strong)LTView *sexView;
@property(nonatomic, strong)LTView *numberView;
@property(nonatomic, strong)LTView *phoneView;
@property(nonatomic, strong)LTView *QQView;
@property(nonatomic, strong)LTView *addressView;
@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.manager = [CoreDataManager defaults];
    
    self.nameView = [[LTView alloc] initWithFrame:CGRectMake(10, 64, 350, 40)];
    [self.view addSubview:self.nameView];
    self.nameView.myLabel.text = @"姓名:";
    self.nameView.myTextField.text = self.name;
    
    self.sexView = [[LTView alloc] initWithFrame:CGRectMake(10, 120, 350, 40)];
    [self.view addSubview:self.sexView];
    self.sexView.myLabel.text = @"性别:";
    self.sexView.myTextField.text = self.sex;
    
    self.numberView = [[LTView alloc] initWithFrame:CGRectMake(10, 180, 350, 40)];
    [self.view addSubview:self.numberView];
    self.numberView.myLabel.text = @"学号:";
    self.numberView.myTextField.text = self.number.stringValue;
    
    self.phoneView = [[LTView alloc] initWithFrame:CGRectMake(10, 240, 350, 40)];
    [self.view addSubview:self.phoneView];
    self.phoneView.myLabel.text = @"电话:";
    self.phoneView.myTextField.text = self.phone;
    
    self.QQView = [[LTView alloc] initWithFrame:CGRectMake(10, 300, 350, 40)];
    [self.view addSubview:self.QQView];
    self.QQView.myLabel.text = @"QQ:";
    self.QQView.myTextField.text = self.QQ;
    
    self.addressView = [[LTView alloc] initWithFrame:CGRectMake(10, 360, 350, 40)];
    [self.view addSubview:self.addressView];
    self.addressView.myLabel.text = @"籍贯:";
    self.addressView.myTextField.text = self.address;
    
    UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [button1 setTitle:@"具体位置" forState:(UIControlStateNormal)];
    button1.frame = CGRectMake(200, 420, 80, 30);
    [self.view addSubview:button1];
    button1.backgroundColor = [UIColor orangeColor];
    [button1 addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [button2 setTitle:@"修改信息" forState:(UIControlStateNormal)];
    button2.frame = CGRectMake(100, 420, 80, 30);
    [self.view addSubview:button2];
    button2.backgroundColor = [UIColor orangeColor];
    [button2 addTarget:self action:@selector(clickButton2:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)clickButton:(UIButton *)button {
    MapViewController *mapVC = [[MapViewController alloc] init];
    [self.navigationController pushViewController: mapVC animated:YES];
    
    
    
    mapVC.address = self.address;
    
}

- (void)clickButton2:(UIButton *)button {
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    NSPredicate *predicate = nil;
    request.predicate = predicate;
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    Student *stu = array[self.i];
//    NSLog(@"%@ %@ %@", stu.name, stu.sex, stu.number);
    stu.name = self.nameView.myTextField.text;
    stu.sex = self.sexView.myTextField.text;
    int str = self.numberView.myTextField.text.intValue;
    stu.number = [NSNumber numberWithInt:str];
    stu.phone = self.phoneView.myTextField.text;
    stu.addressMap = self.addressView.myTextField.text;
    stu.qq = self.QQView.myTextField.text;
    
    [self.manager saveContext];
    
    NSLog(@"修改成功");
    [self.navigationController popViewControllerAnimated:YES];
}



















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
